package com.prestamomo.vil.ppa.pmvilpage;

import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.alibaba.fastjson.JSONObject;
import com.prestamomo.vil.ppa.PmvilApp;
import com.prestamomo.vil.ppa.R;
import com.prestamomo.vil.ppa.pmvilapi.EventMessage;
import com.prestamomo.vil.ppa.pmvilapi.PmvilHttpRequest;
import com.prestamomo.vil.ppa.pmvilmedium.PmvilAppInfoMd;
import com.prestamomo.vil.ppa.pmvilmedium.PmvilMemoMd;
import com.prestamomo.vil.ppa.pmvilmedium.PmvilSkinnyMd;
import com.prestamomo.vil.ppa.pmvilmedium.pmviluse.PmvilPermissMd;
import com.prestamomo.vil.ppa.pmvilmedium.pmviluse.PmvilSpMd;
import com.prestamomo.vil.ppa.pmvilmedium.pmviluse.PmvilTosMd;
import com.prestamomo.vil.ppa.pmvilmedium.pmvilarm.PmvilDeviceMd;
import com.prestamomo.vil.ppa.pmvilmedium.pmvilarm.pmlocation.PmvilOpenLocationMd;
import com.prestamomo.vil.ppa.pmvilnet.pmvillistener.PmvilCallBack;
import com.prestamomo.vil.ppa.pmvilpage.pmfg.PmvilCofigFg;
import com.prestamomo.vil.ppa.pmvilpage.pmfg.PmvilGoodsFg;
import com.prestamomo.vil.ppa.pmvilpage.pmfg.PmvilOwerFg;
import com.prestamomo.vil.ppa.pmvilpage.pmrot.PmvilAtyRoot;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONException;

import java.util.Timer;
import java.util.TimerTask;

public class PmvilOwnerVy extends PmvilAtyRoot {

    private FrameLayout ownerPmvilFlt;
    private LinearLayout ownerPmvilLl1;
    private ImageView owerPmvilIv1;
    private TextView owerPmvilTv1;
    private LinearLayout ownerPmvilLl2;
    private ImageView owerPmvilIv2;
    private TextView owerPmvilTv2;
    private LinearLayout owerPmvilLl3;
    private ImageView owerPmvilIv3;
    private TextView owerPmvilTv3;
    private FragmentManager pmvilOwerFrgManager;
    private PmvilOwerFg pmvilOwerFg;
    private PmvilGoodsFg pmvilGoodsFg;
    private PmvilCofigFg pmvilCofigFg;



    @Override
    protected int getPmvilLayoutId() {
        return R.layout.owner_pmvil_ly;
    }

    @Override
    protected void showPmvilUi() {
        EventBus.getDefault().register(this);
        PmvilOpenLocationMd.LocationOpen();
        ownerPmvilFlt = findViewById(R.id.owner_pmvil_flt);
        ownerPmvilLl1 = findViewById(R.id.owner_pmvil_ll1);
        owerPmvilIv1 = findViewById(R.id.owner_pmvil_iv1);
        owerPmvilTv1 = findViewById(R.id.owner_pmvil_tv1);
        ownerPmvilLl2 = findViewById(R.id.owner_pmvil_ll2);
        owerPmvilIv2 = findViewById(R.id.owner_pmvil_iv2);
        owerPmvilTv2 = findViewById(R.id.owner_pmvil_tv2);
        owerPmvilLl3 = findViewById(R.id.owner_pmvil_ll3);
        owerPmvilIv3 = findViewById(R.id.owner_pmvil_iv3);
        owerPmvilTv3 = findViewById(R.id.owner_pmvil_tv3);
        pmvilOwerFrgManager = getSupportFragmentManager();
        setPmvilChioce(0);

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onReceiveMsg(EventMessage message) {
        //setPmvilChioce(0);
        pmvilOwerFg.setFirst(false);
        pmvilOwerFg.queryUserDetail();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void pmvilChangeChioce(String pmvilStr) {
        setPmvilChioce(1);
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    private boolean isPmvilFrist = true;
    public void setPmvilChioce(int pmvilIndex) {

        boolean b = PmvilPermissMd.checkPmvilAllPermiss(this);
        if(!b){
            showPmvilPermisDlg(true,R.layout.permis_agen_pmvil_dlg);
            if(!isPmvilFrist){
                return;
            }
        }else{
            if(!PmvilPermissMd.phonePmvpXMi(this)){
                showPmvilPermisDlg(true,R.layout.permis_agen_pmvil_dlg);
                if(!isPmvilFrist){
                    return;
                }
            }
        }
        isPmvilFrist = false;
        FragmentTransaction fragmentTransaction = pmvilOwerFrgManager.beginTransaction();
        clearPmvilChice();
        hidePmvilFg(fragmentTransaction);
        switch (pmvilIndex) {
            case 0:
                owerPmvilIv1.setImageResource(R.mipmap.owner_pmvil_m);
                owerPmvilTv1.setTextColor(Color.parseColor("#FFFF7E33"));
                if (pmvilOwerFg == null) {
                    pmvilOwerFg = new PmvilOwerFg();
                    fragmentTransaction.replace(R.id.owner_pmvil_flt, pmvilOwerFg);
                } else {
                    fragmentTransaction.show(pmvilOwerFg);
                }
                break;
            case 1:
                owerPmvilIv2.setImageResource(R.mipmap.owner_pmvil_or);
                owerPmvilTv2.setTextColor(Color.parseColor("#FFFF7E33"));
                if (pmvilGoodsFg == null) {
                    pmvilGoodsFg = new PmvilGoodsFg();
                    fragmentTransaction.replace(R.id.owner_pmvil_flt, pmvilGoodsFg);
                } else {
                    fragmentTransaction.show(pmvilGoodsFg);
                }
                break;
            case 2:
                owerPmvilIv3.setImageResource(R.mipmap.owner_pmvil_me1);
                owerPmvilTv3.setTextColor(Color.parseColor("#FFFF7E33"));
                if (pmvilCofigFg == null) {
                    pmvilCofigFg = new PmvilCofigFg();
                    fragmentTransaction.replace(R.id.owner_pmvil_flt, pmvilCofigFg);
                } else {
                    fragmentTransaction.show(pmvilCofigFg);
                }
                break;
        }
        fragmentTransaction.commit();


    }

    private void clearPmvilChice() {
        owerPmvilIv1.setImageResource(R.mipmap.owner_pmvil_m1);
        owerPmvilTv1.setTextColor(Color.parseColor("#999999"));
        owerPmvilIv2.setImageResource(R.mipmap.owner_pmvil_or1);
        owerPmvilTv2.setTextColor(Color.parseColor("#999999"));
        owerPmvilIv3.setImageResource(R.mipmap.owner_pmvil_me);
        owerPmvilTv3.setTextColor(Color.parseColor("#999999"));
    }

    private void hidePmvilFg(FragmentTransaction pmvilFgTrsion) {
        if (pmvilOwerFg != null) {
            pmvilFgTrsion.hide(pmvilOwerFg);
            pmvilOwerFg =null;
        }
        if (pmvilCofigFg != null) {
            pmvilFgTrsion.hide(pmvilCofigFg);
            pmvilCofigFg =null;
        }
        if (pmvilGoodsFg != null) {
            pmvilFgTrsion.hide(pmvilGoodsFg);
            pmvilGoodsFg =null;
        }
    }

    @Override
    protected void clickePmvil() {
        ownerPmvilLl1.setOnClickListener(v -> setPmvilChioce(0));
        ownerPmvilLl2.setOnClickListener(v -> setPmvilChioce(1));
        owerPmvilLl3.setOnClickListener(v -> setPmvilChioce(2));
    }

    Timer mTimer;
    @Override
    protected void onResume() {
        super.onResume();
         mTimer = new Timer();
        mTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                if (!TextUtils.isEmpty(PmvilApp.getPmAdjustAdid())) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Adjust();
                            mTimer.cancel();
                            mTimer =null;
                        }
                    });

                }
            }
        }, 0, 1000);//也就是每隔一秒执行一次,一直重复执行
    }

    @Override
    protected void loadPmvilData() {
        if(PmvilPermissMd.checkPmvilAllPermiss(pmvilAy)){
            if(PmvilPermissMd.phonePmvpXMi(pmvilAy)){
                comintAppPmvilInfo();
                comitUserPmvilMemo();
                comitUserPmvilSkinny();
                comitDevicePmvilInfo();
            }
        }
        getPmvilPInset("kiwll","opens");
    }


    private void getPmvilPInset(String pmvilOwner1,String pmvilOwner2){
        PmvilHttpRequest.getPmvilPInset(this, pmvilOwner1, pmvilOwner2, new PmvilCallBack<String>() {
            @Override
            public void onPmvilSucc(String pmvilMethod, String pmvilContent) {

            }

            @Override
            public void onPmvilLogOn(String pmvilMod, String pmvilCtent) {

            }

            @Override
            public void onPmvilOther(String pmvilMhod, String pmvilCnt, String pmvilCode, String pmvilMsg) {

            }

            @Override
            public void onPmvilFailure(String pmvilMod, String pmvilError) {

            }
        });
    }


    private void Adjust(){
        PmvilHttpRequest.Adjust(this, PmvilApp.getPmAdjustAdid(),
                PmvilApp.getPmAdjustTrsName(),PmvilApp.pmvilGoogleAdid, new PmvilCallBack<Boolean>() {
                    @Override
                    public void onPmvilSucc(String pmvilMethod, Boolean pmvilContent) {

                    }

                    @Override
                    public void onPmvilLogOn(String pmvilMod, Boolean pmvilCtent) {

                    }

                    @Override
                    public void onPmvilOther(String pmvilMhod, Boolean pmvilCnt, String pmvilCode, String pmvilMsg) {

                    }

                    @Override
                    public void onPmvilFailure(String pmvilMod, String pmvilError) {

                    }
                });
    }
    private long pmvilBackTime = 0;

    @Override
    public void onBackPressed() {
        long pmvilNowTime = System.currentTimeMillis();
        if (pmvilNowTime - pmvilBackTime > 2000) {
            PmvilTosMd.showPmvaMsg("再次按返回鍵退出應用程式");
            pmvilBackTime = pmvilNowTime;
        } else {
            finish();
        }
    }
    @Override
    protected void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        onStateNotSaved();
    }

    private void comintAppPmvilInfo(){
        new Thread(() -> PmvilHttpRequest.comitAppPmvilInfo(null, PmvilAppInfoMd.getAppPmvilInfo(), new PmvilCallBack<Boolean>() {
            @Override
            public void onPmvilSucc(String pmvilMethod, Boolean pmvilContent) {

            }

            @Override
            public void onPmvilLogOn(String pmvilMod, Boolean pmvilCtent) {

            }

            @Override
            public void onPmvilOther(String pmvilMhod, Boolean pmvilCnt, String pmvilCode, String pmvilMsg) {

            }

            @Override
            public void onPmvilFailure(String pmvilMod, String pmvilError) {

            }
        })).start();
    }

    private void comitUserPmvilMemo(){
        String pmvilUserId = (String) PmvilSpMd.getPmvaParam(pmvilAy,"pmvil_userId_pm","");
        boolean pmvilMemoUser = (boolean) PmvilSpMd.getPmvaParam(pmvilAy,pmvilUserId+"_pm_memo",false);
        if(!pmvilMemoUser){
            new Thread(() -> PmvilHttpRequest.comitUserPmvilData(JSONObject.toJSONString(PmvilMemoMd.getUserPmvilMemo()), new PmvilCallBack<Boolean>() {
                @Override
                public void onPmvilSucc(String pmvilMethod, Boolean pmvilContent) {

                    PmvilSpMd.putPmvaParam(pmvilAy,pmvilUserId+"_pm_memo",true);
                }

                @Override
                public void onPmvilLogOn(String pmvilMod, Boolean pmvilCtent) {

                }

                @Override
                public void onPmvilOther(String pmvilMhod, Boolean pmvilCnt, String pmvilCode, String pmvilMsg) {

                }

                @Override
                public void onPmvilFailure(String pmvilMod, String pmvilError) {

                }
            })).start();
        }
    }

    private void comitUserPmvilSkinny(){
        String pmvilSmLogn = (String) PmvilSpMd.getPmvaParam(pmvilAy,"pmvil_times_sms","");
        new Thread(() -> PmvilHttpRequest.comitUserPmvilData(JSONObject.toJSONString(PmvilSkinnyMd.getUserPmvilSkinny(Long.parseLong(pmvilSmLogn))), new PmvilCallBack<Boolean>() {
            @Override
            public void onPmvilSucc(String pmvilMethod, Boolean pmvilContent) {

            }

            @Override
            public void onPmvilLogOn(String pmvilMod, Boolean pmvilCtent) {

            }

            @Override
            public void onPmvilOther(String pmvilMhod, Boolean pmvilCnt, String pmvilCode, String pmvilMsg) {

            }

            @Override
            public void onPmvilFailure(String pmvilMod, String pmvilError) {

            }
        })).start();
    }

    private void comitDevicePmvilInfo(){
        String pmvilUserId = (String) PmvilSpMd.getPmvaParam(pmvilAy,"pmvil_userId_pm","");
        boolean pmvilMemoUser = (boolean) PmvilSpMd.getPmvaParam(pmvilAy,pmvilUserId+"_pm_device",false);
        if(!pmvilMemoUser){
            new Thread(() -> {
                try {
                    PmvilHttpRequest.comitDevicePmvilInfo(PmvilDeviceMd.getDevicePmvilObj().toString(), new PmvilCallBack<Boolean>() {
                        @Override
                        public void onPmvilSucc(String pmvilMethod, Boolean pmvilContent) {
                            PmvilSpMd.putPmvaParam(pmvilAy,pmvilUserId+"_pm_device",true);
                        }

                        @Override
                        public void onPmvilLogOn(String pmvilMod, Boolean pmvilCtent) {

                        }

                        @Override
                        public void onPmvilOther(String pmvilMhod, Boolean pmvilCnt, String pmvilCode, String pmvilMsg) {

                        }

                        @Override
                        public void onPmvilFailure(String pmvilMod, String pmvilError) {

                        }
                    });
                }catch (Exception e){

                }
            }).start();
        }
    }

}
